<?php
namespace App\Http\Controllers\admin_system;


use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\V1\CompanyModel;
use App\Models\V1\MemberModel;
use App\Models\V1\ReadPartyModel;



use App\Common\ApiData;
use DB;

class CompanyController extends Controller{

    private  $company;
    public function __construct(){
        $this->company = new CompanyModel();
    }
    /*
     * 公司列表
     * */
    public function company_list(Request $request){
        $keywords = trim($request->input('keywords'));
        $companys = $this->company->get_company_list($keywords);

        foreach($companys as $com){
            $companyId          = $com->company_id;
            $com->readPartys    = $this->company->get_company_read_partys($companyId);
            $com->countEmployee = $this->company->count_company_member($companyId);
        }

        $data   = [
            'companys'=>    $companys,
            'keywords'=>    $keywords
        ];
        return view('admin_system/company/company-list',$data);
    }



    /*
     * 编辑、添加新公司
     * */
    public function company_detail(Request $request){
        $companyId = $request->input('companyId',0);
        $companyInfo = $this->company->get_company_info($companyId);

        if(!$companyInfo){
            $companyInfo = new \stdClass();
            $companyInfo->company_id    = 0;
            $companyInfo->company_name  = "";
            $companyInfo->vip_port      = 0;
            $companyInfo->vip_begin_date = current_date();
            $companyInfo->vip_end_date   = current_date();
            $companyInfo->manager_user_id= 0;
            $companyInfo->surplus_port  = 0;
        }

        if($companyInfo->manager_user_id > 0){
            $memberModel = new MemberModel();
            $memberInfo  = $memberModel->get_member_info_by_id($companyInfo->manager_user_id);
            $companyInfo->mobile = $memberInfo->UserName;
        }else{
            $companyInfo->mobile = "";
        }
        return view('admin_system/company/company-detail',['company'=>$companyInfo]);
    }


    /*
     * 更新企业信息
     * */
    public function company_update(Request $request){

        $companyId  = $request->input('company_id');
        $mobile     = $request->input('mobile');
        $memberModel = new MemberModel();
        if($mobile){
            $memberInfo  = $memberModel->get_member_info_by_mobile($mobile);
            $userId      = $memberInfo->UserID;
        }else{
            $userId = 0;
        }


        $data = [
            'company_name'  => $request->input('company_name'),
            'vip_port'      => $request->input('vip_port'),
            'surplus_port'  => $request->input('surplus_port'),
            'vip_begin_date'=> $request->input('vip_begin_date'),
            'vip_end_date'  => $request->input('vip_end_date'),
            'manager_user_id'=>$userId,
            'company_id'    => $companyId,
        ];
        
        if($companyId > 0) //编辑
        { 
            $result = $this->company->update_company($data);

            //同时修改该公司下的所有会员的有效时间
            $members = $this->company->get_company_members($companyId,'',1000);
            foreach($members as $member)
            {
                $memberModel->change_member_endtime($member->UserID);
            }
        }else{ //新添加
            $result = $this->company->add_company($data);
        }

        if($result){ //正确 返回列表界面
            return redirect('/admin_system/company/company_list');
        }else{      //错误 返回编辑界面
            return redirect()->back()->withInput()->withErrors('保存失败！');
        }

    }

    /**
     * 删除公司
     * */
    public function company_delete(Request $request){
        $companyId  = $request->get('companyId');
        $companyModel   = new CompanyModel();
        $companyModel->delete_company($companyId);
        return redirect()->back();
    }


    /**
     * 更新公司部门信息
     *
     * */
    public function department_update(Request $request){
        $data = [
            'department_id' => $request->input('department_id',0),
            'company_id'    => $request->input('company_id',0),
            'department_name'=>$request->input('department_name','')
        ];
        $result = $this->company->update_department($data);
        return redirect()->back();
    }

    /*
     * 删除部门
     * */
    public function department_delete(Request $request){
        $departmentId = $request->input('departmentId');
        $this->company->department_delete($departmentId);
        //把会员移除部门
        DB::table('memberinfo')
            ->where('department_id',$departmentId)
            ->update(['department_id'=>0]);
        return redirect()->back();
    }


    /**
    * 获得公司信息 以API形式
    */
    public function get_company_info(Request $request){
        $companyName    = $request->input('companyName');
        $apiData        = new Apidata();
        $companyModel   = new CompanyModel();
        $companyInfo    = $companyModel->get_company_info_by_name($companyName);
        if($companyInfo == false){
            $apiData->message   = "公司不存在";
            $apiData->code      = 4001;
        }else{
            $apiData->message   = "ok";
            $apiData->code      = 200;
            $apiData->set_data('companyInfo',$companyInfo);
        }
        return $apiData->send();
    }

    /*
     * 导入单个会员
     * */
    public function load_member(Request $request){
        $companyId      = $request->input('companyId');
        $companyModel   = new CompanyModel();
        $companyInfo    = $companyModel->get_company_info($companyId);

        //获取公司的所有读书会
        $readpartys     = $companyModel->get_company_read_partys($companyId);
        $departments    = $companyModel->get_company_department($companyId);


        $data = [
            'companyInfo'   => $companyInfo,
            'departments'   => $departments,
            'readpartys'    => $readpartys
        ];

        return view('admin_system/company/load-member',$data);
    }


    /*
     * 导入单个会员  实体操作
     * */
    public function load_member_action(Request $request){
        $userId     = $request->input('userId');
        $companyId  = $request->input('companyId');
        $readPartyId= $request->input('readPartyId');
        $departmentId=$request->input('departmentId');
        $job        = $request->input('job');
        $isManager  = $request->input('isManager');


        $apiData    =   new ApiData();
        $companyModel=  new CompanyModel();
        $memberModel =  new MemberModel();

        $memberInfo  =  $memberModel->get_member_info_by_id($userId);

        $isExists   = $companyModel->company_exists_member($companyId,$memberInfo->UserID);

        //1.检查该用户是否已属于该公司
        if($isExists){
            return $apiData->send(2000,'该用户已是公司员工');
        }

        //修改用户信息
        $memberInfo = [
            'States'        => 1,
            'UserID'        => $userId,
        ];
        $memberModel->update_member_info($memberInfo);

    
        //修改公司的管理员ID
        if($isManager == 1){
            $companyInfo    = [
                'company_id'        =>  $companyId,
                'manager_user_id'   =>  $userId
            ];
            $companyModel->update_company($companyInfo);
        }

        $companyModel->add_company_member($companyId,$departmentId,$userId,$job);   //加入企业
        $memberModel->change_member_endtime($userId);                              //修改时间

        $giftRight      = $request->input('giftRight',0);
        $readpartyModel = new ReadPartyModel();
        $readpartyModel->add_read_party_member($readPartyId,$userId,$departmentId,['look_gift_right'=>$giftRight]);

        //使用端口
        $companyModel->use_company_port($companyId,1);

        $apiData->message = "导入成功";
        $apiData->code  = 200;
        return $apiData->send();
    }


    /**
     * 公司员工列表
     * */
    public function members(Request $request){

        $companyId  = trim($request->input('companyId'));
        $keywords   = trim($request->input('keywords'));
        $members        = $this->company->get_company_members($companyId,$keywords);
        foreach($members as $member)
        {
            $reads  = explode(',',$member->ReaParName);
            $parts  = explode(',',$member->department_name);
            $readpartys = [];
            foreach($reads as $k => $v)
            {
                array_push($readpartys,"【{$v}=>{$parts[$k]}】");
            }
            $member->readpartys = $readpartys;
        }
        return view('admin_system/company/members',['members'=>$members,'keyword'=>$keywords,'companyId'=>$companyId]);
    }


    /**
     * 退出公司
     * */
    public function quit_company(Request $request){
        $userId     = $request->input('userId');
        $companyId  = $request->input('companyId');
        $apiData    = new ApiData();

        $companyModel   = new CompanyModel();
        $companyModel->delete_company_member($companyId,$userId);
        $memberModel    = new MemberModel();
        $memberModel->change_member_endtime($userId);
        $apiData->message   = "删除成功";
        $apiData->code      = 200;
        return $apiData->send();
    }
}